package com.itestor.iptv.modules.ne.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.global.exception.WebApplicationException;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.utils.NeConstant;
import com.itestor.iptv.modules.ne.vo.RobotCFGOBJ;
import com.itestor.iptv.modules.ne.vo.RobotFileInfoOBJ;
import com.itestor.iptv.modules.ne.vo.RobotManageOBJ;
import com.itestor.iptv.modules.ne.vo.RobotNetInfoOBJ;

public class RobotManageAddAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RobotManageAddAction.class);

    /**
     * 
     */
    private RobotManageService robotManageService;
    /**
     * 
     */
    private RegionManageService regionManageService;

    /**
     * @param mapping struts mapping
     * @param request request
     * @param form action form
     * @param response response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward addRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = request.getParameter("name");
        String channel = request.getParameter("channel");        
        String region = request.getParameter("region");
        String subregion = request.getParameter("subregion");

        RobotManageOBJ bean = new RobotManageOBJ();
        if (StringUtils.isEmpty(name)) {
            throw new WebApplicationException("global.error.submit",
                    GlobalConstant.PROPERTIES_BUNDLE);
        }
        bean.setName(name);
        
        bean.setChannel(Integer.parseInt(channel));
        
        if (!StringUtils.isEmpty(subregion)) {
            RegionManageOBJ regionManageOBJ=regionManageService.get(Integer.parseInt(subregion));
            bean.setRegionManageOBJ(regionManageOBJ);
        } else if(!StringUtils.isEmpty(region)){
            RegionManageOBJ regionManageOBJ=regionManageService.get(Integer.parseInt(region));
            bean.setRegionManageOBJ(regionManageOBJ);
        }

        if (null != robotManageService.get(name).getName()) {
            throw new WebApplicationException("common.region.error.existed",
                    CommonConstant.PROPERTIES_BUNDLE);
        }
        
        robotManageService.add(bean);
        
        //request.setAttribute("id", robotManageService.get(name).getIdRobot());
        return mapping.findForward(NeConstant.FORWARD_ROBOT_ADD);
    }

    public ActionForward addCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");

        RobotCFGOBJ region = new RobotCFGOBJ();
        if (!StringUtils.isEmpty(request.getParameter("id")))
            region.setIdRobot(Integer.valueOf(request.getParameter("id")));
        if (!StringUtils.isEmpty(request.getParameter("logAfterSec")))
            region.setLogAfterSec(Integer.valueOf(request
                    .getParameter("logAfterSec")));
        if (!StringUtils.isEmpty(request.getParameter("logBeforeSec")))
            region.setLogBeforeSec(Integer.valueOf(request
                    .getParameter("logBeforeSec")));
        if (!StringUtils.isEmpty(request.getParameter("lostRateBaseUnit")))
            region.setLostRateBaseUnit(Integer.valueOf(request
                    .getParameter("lostRateBaseUnit")));
        if (!StringUtils.isEmpty(request.getParameter("qualityBaseUnit")))
            region.setQualityBaseUnit(Integer.valueOf(request
                    .getParameter("qualityBaseUnit")));
        if (!StringUtils.isEmpty(request.getParameter("recyleLogClearCycle")))
            region.setRecyleLogClearCycle(Integer.valueOf(request
                    .getParameter("recyleLogClearCycle")));
        if (!StringUtils.isEmpty(request.getParameter("unuserLogClearCycle")))
            region.setUnuserLogClearCycle(Integer.valueOf(request
                    .getParameter("unuserLogClearCycle")));

        LoggerUtils.logActionInfo("Add the RobotCFG - id: " + id, logger,
                request);

        robotManageService.add(region);

        return mapping.findForward(NeConstant.FORWARD_CFG_ADD);
    }

    public ActionForward addFile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        RobotFileInfoOBJ region = new RobotFileInfoOBJ();

        if (!StringUtils.isEmpty(request.getParameter("id")))
            region.setIdRobot(Integer.valueOf(request.getParameter("id")));
        region.setPcapFileDir(request.getParameter("pcapFileDir"));
        region.setPcapUrlDir(request.getParameter("pcapUrlDir"));
        region.setVideoFileDir(request.getParameter("videoFileDir"));
        region.setVideoPicFileDir(request.getParameter("videoPicFileDir"));
        region.setVideoUrl(request.getParameter("videoUrl"));
        region.setVideoUrlDir(request.getParameter("videoUrlDir"));
        region.setWebHost(request.getParameter("webHost"));
        // request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, region);

        LoggerUtils.logActionInfo("Add the RobotFileInfo - id: " + id, logger,
                request);

        robotManageService.add(region);

        return mapping.findForward(NeConstant.FORWARD_FILE_ADD);
    }

    public ActionForward addNet(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        RobotNetInfoOBJ region = new RobotNetInfoOBJ();

        if (!StringUtils.isEmpty(request.getParameter("id")))
            region.setIdRobot(Integer.valueOf(request.getParameter("id")));
        region.setComPort(request.getParameter("comPort"));
        region.setIpAddress(request.getParameter("ipAddress"));
        if (!StringUtils.isEmpty(request.getParameter("irAgentPort")))
            region.setIrAgentPort(Integer.valueOf(request
                    .getParameter("irAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("irPort")))
            region.setIrPort(Integer.valueOf(request.getParameter("irPort")));
        if (!StringUtils.isEmpty(request.getParameter("picAgentPort")))
            region.setPicAgentPort(Integer.valueOf(request
                    .getParameter("picAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("qosAgentPort")))
            region.setQosAgentPort(Integer.valueOf(request
                    .getParameter("qosAgentPort")));
        if (!StringUtils.isEmpty(request.getParameter("videoAgentPort")))
            region.setVideoAgentPort(Integer.valueOf(request
                    .getParameter("videoAgentPort")));
        region.setRobotDesc(request.getParameter("robotDesc"));

        LoggerUtils.logActionInfo("Add the RobotNetInfo - id: " + id, logger,
                request);

        robotManageService.add(region);

        return mapping.findForward(NeConstant.FORWARD_NET_ADD);
    }

    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }

    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }
    
    

}
